package com.taokatao.cardmee.business.carddetail;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.api.model.CardFDMyCourseInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CardFDBookDateLayout extends LinearLayout implements View.OnClickListener {

    @BindView(R.id.l_time_1) View lTime1;
    @BindView(R.id.i_state_1) ImageView iState1;
    @BindView(R.id.t_time_1_1) TextView tTime11;
    @BindView(R.id.t_time_1_2) TextView tTime12;
    @BindView(R.id.l_time_2) View lTime2;
    @BindView(R.id.i_state_2) ImageView iState2;
    @BindView(R.id.t_time_2_1) TextView tTime21;
    @BindView(R.id.t_time_2_2) TextView tTime22;
    @BindView(R.id.l_time_3) View lTime3;
    @BindView(R.id.i_state_3) ImageView iState3;
    @BindView(R.id.t_time_3_1) TextView tTime31;
    @BindView(R.id.t_time_3_2) TextView tTime32;
    @BindView(R.id.l_time_4) View lTime4;
    @BindView(R.id.i_state_4) ImageView iState4;
    @BindView(R.id.t_time_4_1) TextView tTime41;
    @BindView(R.id.t_time_4_2) TextView tTime42;
    @BindView(R.id.l_time_5) View lTime5;
    @BindView(R.id.i_state_5) ImageView iState5;
    @BindView(R.id.t_time_5_1) TextView tTime51;
    @BindView(R.id.t_time_5_2) TextView tTime52;

    List<ImageView> iFlagList = new ArrayList<>();
    final String[] dayOfWeeks = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    final Calendar calendar;
    Date firstDate;
    int selectedIndex = 1;

    public CardFDBookDateLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        ButterKnife.bind(this, LayoutInflater.from(context).inflate(R.layout.layout_card_fd_book_date, this));
        calendar = Calendar.getInstance();
        iFlagList.add(iState1);
        iFlagList.add(iState2);
        iFlagList.add(iState3);
        iFlagList.add(iState4);
        iFlagList.add(iState5);
    }

    public String getSelectedDate() {
        if (firstDate != null) {
            calendar.setTime(firstDate);
            calendar.add(Calendar.DATE, selectedIndex - 1);
            Date date2 = calendar.getTime();
            return sdf.format(date2);
        }
        return "";
    }

    public void refreshOrderFlag(List<CardFDMyCourseInfo> dataList) {
        if (firstDate == null) return;

        Utils.logd("iFlagList.size():" + iFlagList.size());
        for (int i = 0; i < iFlagList.size(); i++) {
            calendar.setTime(firstDate);
            calendar.add(Calendar.DATE, i);
            Date date = calendar.getTime();
            whileCheck(dataList, sdf.format(date), iFlagList.get(i));
        }
    }

    private void whileCheck(List<CardFDMyCourseInfo> dataList, String date, ImageView i) {

        boolean handle = false;

        if (dataList != null) {
            Utils.logd("whileCheck  dataList.size():" + dataList.size() + "##date:" + date + "imageid:" + i.getId());
            for (CardFDMyCourseInfo cci : dataList) {
                if (String.valueOf(cci.classTime).startsWith(date)) {
                    Utils.logd("whileCheck date:" + date + "##找到当天数据!! cci.status:" + cci.status);
                    if (cci.status == CardFDMyCourseInfo.STATUS_ORDERED) {
                        i.setVisibility(View.VISIBLE);
                        handle = true;
                        break;
                    }
                }
            }
        }

        if (!handle) i.setVisibility(View.GONE);
    }

    /**
     * @param time yyyyMMdd 格式
     */
    public void setData(String time) {
        try {
            firstDate = sdf.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (firstDate == null) return;
        Utils.logd("CardFDBookDateLayout setData firstDate:" + firstDate);
        calendar.setTime(firstDate);

        String sMonth1 = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        String sDay1 = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        tTime11.setText(sMonth1 + "-" + sDay1);
        tTime12.setText(dayOfWeeks[calendar.get(Calendar.DAY_OF_WEEK) - 1]);

        calendar.add(Calendar.DATE, 1);
        String sMonth2 = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        String sDay2 = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        tTime21.setText(sMonth2 + "-" + sDay2);
        tTime22.setText(dayOfWeeks[calendar.get(Calendar.DAY_OF_WEEK) - 1]);

        calendar.add(Calendar.DATE, 1);
        String sMonth3 = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        String sDay3 = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        tTime31.setText(sMonth3 + "-" + sDay3);
        tTime32.setText(dayOfWeeks[calendar.get(Calendar.DAY_OF_WEEK) - 1]);

        calendar.add(Calendar.DATE, 1);
        String sMonth4 = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        String sDay4 = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        tTime41.setText(sMonth4 + "-" + sDay4);
        tTime42.setText(dayOfWeeks[calendar.get(Calendar.DAY_OF_WEEK) - 1]);

        calendar.add(Calendar.DATE, 1);
        String sMonth5 = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        String sDay5 = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        tTime51.setText(sMonth5 + "-" + sDay5);
        tTime52.setText(dayOfWeeks[calendar.get(Calendar.DAY_OF_WEEK) - 1]);

        // 选中第一个
        selectedIndex = 1;
        lTime1.performClick();
    }

    @Override
    @OnClick({R.id.l_time_1, R.id.l_time_2, R.id.l_time_3, R.id.l_time_4, R.id.l_time_5,})
    public void onClick(View v) {
        if (v == lTime1) {
            lTime1.setBackgroundResource(R.drawable.shape_card_fd_book_date_selected);
            lTime2.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime3.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime4.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime5.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);

            iState1.setImageResource(R.mipmap.card_fd_right_selected);
            iState2.setImageResource(R.mipmap.card_fd_right_normal);
            iState3.setImageResource(R.mipmap.card_fd_right_normal);
            iState4.setImageResource(R.mipmap.card_fd_right_normal);
            iState5.setImageResource(R.mipmap.card_fd_right_normal);

            if (cb != null && selectedIndex != 1) {
                cb.onNewDateClick(sdf.format(firstDate));
            }
            selectedIndex = 1;
        } else if (v == lTime2) {
            lTime1.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime2.setBackgroundResource(R.drawable.shape_card_fd_book_date_selected);
            lTime3.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime4.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime5.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);

            iState1.setImageResource(R.mipmap.card_fd_right_normal);
            iState2.setImageResource(R.mipmap.card_fd_right_selected);
            iState3.setImageResource(R.mipmap.card_fd_right_normal);
            iState4.setImageResource(R.mipmap.card_fd_right_normal);
            iState5.setImageResource(R.mipmap.card_fd_right_normal);

            if (cb != null && selectedIndex != 2) {
                calendar.setTime(firstDate);
                calendar.add(Calendar.DATE, 1);
                Date date2 = calendar.getTime();
                cb.onNewDateClick(sdf.format(date2));
            }
            selectedIndex = 2;

        } else if (v == lTime3) {
            lTime1.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime2.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime3.setBackgroundResource(R.drawable.shape_card_fd_book_date_selected);
            lTime4.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime5.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);

            iState1.setImageResource(R.mipmap.card_fd_right_normal);
            iState2.setImageResource(R.mipmap.card_fd_right_normal);
            iState3.setImageResource(R.mipmap.card_fd_right_selected);
            iState4.setImageResource(R.mipmap.card_fd_right_normal);
            iState5.setImageResource(R.mipmap.card_fd_right_normal);

            if (cb != null && selectedIndex != 3) {
                calendar.setTime(firstDate);
                calendar.add(Calendar.DATE, 2);
                Date date3 = calendar.getTime();
                cb.onNewDateClick(sdf.format(date3));
            }
            selectedIndex = 3;

        } else if (v == lTime4) {
            lTime1.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime2.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime3.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime4.setBackgroundResource(R.drawable.shape_card_fd_book_date_selected);
            lTime5.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);

            iState1.setImageResource(R.mipmap.card_fd_right_normal);
            iState2.setImageResource(R.mipmap.card_fd_right_normal);
            iState3.setImageResource(R.mipmap.card_fd_right_normal);
            iState4.setImageResource(R.mipmap.card_fd_right_selected);
            iState5.setImageResource(R.mipmap.card_fd_right_normal);

            if (cb != null && selectedIndex != 4) {
                calendar.setTime(firstDate);
                calendar.add(Calendar.DATE, 3);
                Date date4 = calendar.getTime();
                cb.onNewDateClick(sdf.format(date4));
            }
            selectedIndex = 4;

        } else if (v == lTime5) {
            lTime1.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime2.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime3.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime4.setBackgroundResource(R.drawable.shape_card_fd_book_date_normal);
            lTime5.setBackgroundResource(R.drawable.shape_card_fd_book_date_selected);
            iState1.setImageResource(R.mipmap.card_fd_right_normal);
            iState2.setImageResource(R.mipmap.card_fd_right_normal);
            iState3.setImageResource(R.mipmap.card_fd_right_normal);
            iState4.setImageResource(R.mipmap.card_fd_right_normal);
            iState5.setImageResource(R.mipmap.card_fd_right_selected);


            if (cb != null && selectedIndex != 5) {
                calendar.setTime(firstDate);
                calendar.add(Calendar.DATE, 4);
                Date date5 = calendar.getTime();
                cb.onNewDateClick(sdf.format(date5));
            }
            selectedIndex = 5;
        }
    }


    public interface Callback {
        void onNewDateClick(String dateTime);
    }

    Callback cb;

    public void setCallback(Callback callback) {
        cb = callback;
    }
}
